package com.frkj.yuan.base;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.frkj.yuan.R;

import butterknife.ButterKnife;


/**
 * 所有fragment 的基类 封装了跳转动画
 * @author Administrator
 */
public abstract class BaseFragment extends Fragment {
    private View rootView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(getLayoutResource(), container, false);
            ButterKnife.bind(this, rootView);
            this.initLayoutView();

        }
        return rootView;
    }
    /**
     *  获取布局文件
     */
    public abstract int getLayoutResource();
    /**
     *  初始化view
     */
    public abstract void initLayoutView();

    /**
     * 通过类名启动Activity
     *
     * @param pClass
     */
    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }
    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(intent);
    }
    /**
     * 通过类名启动Activity带动画
     * @param pClass
     */
    public void openActivityWitchAnimation(Class<?> pClass) {
        openActivityWitchAnimation(pClass, null);
    }
    /**
     * 通过类名启动Activity，并且含有Bundle数据,带动画
     *
     * @param pClass
     * @param pBundle
     */
    public void openActivityWitchAnimation(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(intent);
        //跳转动画
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        }
    }
    /**
     * 带动画的退出
     */
    public void finishwitchAnimation() {
        getActivity().finish();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            getActivity().overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        }
    }
}
